﻿using MyApplication;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;

namespace MyWeb.App_Start
{
    public interface ISchedulerJob
    {
        void Execute();
    }
    public class SampleJob : ISchedulerJob
    {
        public void Execute()
        {
            //文件保存的物理路径，CSTest为虚拟目录名称，F:\Inetpub\wwwroot\CSTest为物理路径
            AutofacExt.GetFromFac<VisitorApp>().DeleteVisitorOutTime();
        }
    
    }
    public class SchedulerConfiguration
    {
        //时间间隔
        private int sleepInterval;
        //任务列表
        private ArrayList jobs = new ArrayList();

        public int SleepInterval { get { return sleepInterval; } }
        public ArrayList Jobs { get { return jobs; } }

        //调度配置类的构造函数
        public SchedulerConfiguration(int newSleepInterval)
        {
            sleepInterval = newSleepInterval;
        }
    }
    public class Scheduler
    {
        private SchedulerConfiguration configuration = null;

        public Scheduler(SchedulerConfiguration config)
        {
            configuration = config;
        }

        public void Start()
        {
            while (true)
            {
                //执行每一个任务
                foreach (ISchedulerJob job in configuration.Jobs)
                {
                    ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
                    Thread myThread = new Thread(myThreadDelegate);
                    myThread.Start();
                    Thread.Sleep(configuration.SleepInterval);
                }
            }
        }
    }
}